package com.johnsoft.pph;

import java.io.File;
import java.io.IOException;

import com.johnsoft.util.ZIP;
/**
 * InstallThread.java is executing the installation and forwarding output to the "main" thread which is updating the console.
 * This is done to keep the console alive (not freeze / not responding) during installation.
 * @author Johnnei
 *
 */
public class InstallerThread extends Thread {
	
	public void run()
	{
		try {
			Sleep(500);
			log("Starting The Pre-PreHistoric Mod Installation... (" + Installer.setup.installFile.substring(0, Installer.setup.installFile.length() - 4) + ")");
			Setup setup = new Setup();
			CabReader cab = new CabReader();
			
			/**
			 * Read the Client or Server Side Data Byte
			 */
			setup.setIsClient(cab.readByte() == 1 ? true : false);
			if(setup.isClientMod())
				log("Installing Client Mod of the Pre-PreHistoric Mod");
			else
				log("Installing Server Mod of the Pre-PreHistoric Mod");
			
			/**
			 * Read the Amount of Supported Versions and their Version Hashes
			 */
			setup.setVersionCount(cab.readByte());
			log("This version of The Pre-PreHistoric Mod has " + setup.getSupportedVersions() + " supported versions");
			for(int i = 0; i < setup.getSupportedVersions(); i++) {
				setup.setVersionHash(i, cab.readString());
				log("Version \""+setup.getVersionHash(i)+"\" support has been found");
			}
			
			/**
			 * Detect Minecraft Installed Version and Check if its supported by the setup
			 */
			setup.detectMinecraftInstall();
			log("Installed Version of minecraft: " + setup.getInstalledVersion());
			if(!setup.canInstall()) {
				log("You have an un supported version installed of minecraft! (" + setup.getInstalledVersion() + ")");
				log("This version is the last downloaded version with the minecraft launcher! (.minecraft/bin/version)");
				log("Installation Failed (Error 0: Version Not Supported)");
				return;
			}
			log("Your version of minecraft is supported.");
			
			/**
			 * Grab and Extract Minecraft.jar
			 */
			log("Copying Minecraft.jar");
			setup.grabJar();
			log("Extracting Minecraft.jar");
			ZIP.unpackData(new File("minecraft.zip"), new File("temp\\"));
			
			/**
			 * Unpack the data from the setup cab
			 */
			log("Unpacking Setup...");
			cab.unpack("pph");
			cab.close(); //No more need of this file, Unload it from memory
			
			/**
			 * Check for ModLoader and ModLoaderMP
			 */
			log("Checking for ModLoader and ModLoaderMP");
			setup.installModLoader();
			setup.installModLoaderMP();
			
			/**
			 * Install Mod and remove META-INF folders if present
			 */
			log("Installing The Pre-PreHistoric Mod");
			setup.installMod();
			setup.removeMeta();
			
			/**
			 * Zip minecraft back up
			 */
			log("Packing Minecraft.jar");
			ZIP.packData(new File("temp\\"), "minecraft.jar.zip");
			
			/**
			 * Restore minecraft into its normal dir as a .jar file
			 */
			log("Copying jar to minecraft");
			setup.restoreJar();
			
			/**
			 * Clean up the TEMP files
			 */
			log("Cleaning installation data...");
			ZIP.deleteFolder(new File("temp\\"));
			ZIP.deleteFolder(new File("pph\\"));
			new File("minecraft.zip").delete();
			new File("minecraft.jar.zip").delete();
			log("Installation Completed!");
			log("You can now enjoy the wonderfull mod: The Pre-PreHistoric Mod");
			log("You can also close this screen now. Have Fun from the PPH Mod Team!");
			
			
		} catch (IOException e) {
			log("Setup encountered an error! Please retry the installation!");
			log("ErrMsg: " + e.getMessage());
		}
	}
	
	public static void log(String s)
	{
		Installer.setup.addConsoleText(s);
	}
	
	public static void Sleep(int ms)
	{
		try {
			sleep(ms);
		} catch (Exception e)
		{
		}
	}

}
